<?php

//泡泡功能
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Common\push;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BubbleController extends  Controller{

    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址

    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址*/


    //统计这个用户下有多少没读
    //统计有多少点赞多少条未读
    public function amount(Request $request){
        if($request ->isMethod('post')){
            $user_id       = $request ->input('user_id');
            $select = DB::table('release')->select('release_id')->where('user_release', $user_id)->get();
            $arrs = (object)array();
            if (empty($select)) {
                return json::json('failure', '没有评论信息', $arrs);
            } else {
                $arr = array();
                foreach ($select as $value) {
                    $data = DB::table('release_topic')->select('releasetopic_id')->where('release_releasetopic', $value->release_id)->where('operate', 0)->get();
                    foreach ($data as $key) {
                        $id = $key->releasetopic_id;
                        $arr[] = $id;
                    }
                }
                $arlike = array();
                foreach ($select as $value) {
                    $datalike = DB::table('like')->select('like_id')->where('release_like', $value->release_id)->where('operate', 0)->get();
                    foreach ($datalike as $key) {
                        $id = $key->like_id;
                        $arlike[] = $id;
                    }
                }
                $sumdata = count($arr) + count($arlike);
                if ($sumdata == 0) {
                    return json::json('failure', '没有评论信息', $arrs);
                } else {

                    /*                    $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                                        $pushios->send_pub($datselect->phone, '你有' . $sumdata . '未读信息', '8', $sumdata);*/
                    return json::json('success', "你有条信息", array('sum' => $sumdata));
                   /* $datselect = DB::table('user')->select('phone')->where('user_id', $user_id)->first();
                    $push = new push($this->app_key, $this->master_secret, $this->url);
                    $push->send_pub($datselect->phone, '你有' . $sumdata . '未读信息', '8', $sumdata);
                    $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                    $pushios->send_pub($datselect->phone, '你有' . $sumdata . '未读信息', '8', $sumdata);*/
                }
            }
        }
    }

    public function Releaselike($release_id){
        $data = DB::table('like')
            ->leftjoin('user','like.user_like','=','user.user_id')
            ->leftjoin('release','like.release_like','=','release.release_id')
            ->where('release_like',$release_id)
            ->where('operate',0)
            ->get();
        return $data;

    }

    public  function second($release_id){
        $data = DB::table('release_topic')
            ->select('release_id','head','nickname','releasetopic_content','userID','releasetopic_id','release_releasetopic','user_releasetopic','release_operation')
            ->leftjoin('user','release_topic.user_releasetopic','=','user.user_id')
            ->leftjoin('release','release_topic.release_releasetopic','=','release.release_id')
            ->where('release_releasetopic',$release_id)
            ->where('operate',0)
            ->orderby('releasetopic_id','ASE')
            ->get();
        return $data;

    }






    //显示与自己想送的信息展示
    public function display(Request $request){
        if($request ->isMethod('post')) {
            $userid = $request->input('user_id');
            $data = DB::table('release')->select('release_id')->where('user_release', $userid)->get();
            if (empty($data)) {
                return json::json('failure', '没有评论信息', []);
            } else {
                foreach ($data as $value) {
                    $id = $value->release_id;
                    $di = explode(',', $id);
                    foreach ($di as $k => $v) {
                        $likes[] = $this->Releaselike($v);
                        $dd[] = $this->second($v);
                    }
                }
                if (empty($dd)) {
                    $ddsar = array();
                } else {
                    $dht = array_values(array_filter($dd));
                    $result = array_reduce($dht, 'array_merge', array());
                    foreach ($result as $key) {
                        $releasetopic_id[] = $key->releasetopic_id;
                        $release_id = $key->release_id;
                        $releasetopic_content = $key->releasetopic_content;
                        $release_operation = $key->release_operation;

                        if ($release_operation == 1 ) {
                            $head = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                            $nickname = "匿名";
                        } else {
                            $head = $key->head;
                            $nickname = $key->nickname;
                        }
                        $dds[] = array(
                            'release_id' => $release_id,
                            'head' => $head,
                            'nickname' => $nickname,
                            'content' => $releasetopic_content);
                    }
                    if (empty($dds)) {
                        $ddsar = array();
                    } else {
                        $ddsar = $dds;
                    }
                }
                if (empty($likes)) {
                    $ddlike = array();
                } else {
                    $dhtlike = array_values(array_filter($likes));
                    $resultlike = array_reduce($dhtlike, 'array_merge', array());
                    foreach ($resultlike as $lik) {
                        $like_id[] = $lik->like_id;
                        $release_id = $lik->release_id;
                        $release_operation = $lik->release_operation;
                        if ($release_operation == 1 ) {
                            $heads = "http://www.mukitech.com/wuye_backend/upload/image/rotation_img310447598.jpg";
                            $user_id = "匿名";
                        } else {
                            $heads = $lik->head;
                            $user_id = $lik->nickname;
                        }
                        $lk[] = array(
                            'release_id' => $release_id,
                            'head' => $heads,
                            'nickname' => $user_id,
                            'content' => ""
                        );
                    }
                    if (empty($lk)) {
                        $ddlike = array();
                    } else {
                        $ddlike = $lk;
                    }
                }

                $kk = array_merge($ddsar, $ddlike);
            }
            if (!empty($kk)) {
                if (!empty($releasetopic_id)) {
                    foreach ($releasetopic_id as $k => $v) {
                        DB::table('release_topic')->where('releasetopic_id', $v)->update(['operate' => 1]);
                    }
                }
                if (!empty($like_id)) {
                    foreach ($like_id as $l => $s) {
                        DB::table('like')->where('like_id', $s)->update(['operate' => 1]);
                    }
                }
                return json::json('success', "评论信息展示", $kk);
            } else {
                return json::json('failure', '没有评论信息', []);
            }
        }
    }


}